package com.monitor.ui;

import com.monitor.security.PasswordManager;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * 维护中心面板，包含密码修改、系统版本信息和更新功能
 */
public class MaintenancePanel extends JPanel {
    private static final Color MAIN_COLOR = new Color(0, 120, 215);
    
    private PasswordManager passwordManager;
    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField;
    private JPasswordField confirmPasswordField;
    
    public MaintenancePanel() {
        // 初始化密码管理器
        passwordManager = new PasswordManager();
        
        // 设置面板
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        
        // 创建标题面板
        JPanel titlePanel = createTitlePanel("维护中心");
        add(titlePanel, BorderLayout.NORTH);
        
        // 创建内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 创建密码修改面板
        JPanel passwordPanel = createPasswordPanel();
        
        // 创建版本信息面板
        JPanel versionPanel = createVersionPanel();
        
        // 添加到内容面板
        contentPanel.add(passwordPanel);
        contentPanel.add(Box.createVerticalStrut(20));
        contentPanel.add(versionPanel);
        contentPanel.add(Box.createVerticalGlue());
        
        // 添加滚动面板
        JScrollPane scrollPane = new JScrollPane(contentPanel);
        scrollPane.setBorder(null);
        add(scrollPane, BorderLayout.CENTER);
    }
    
    private JPanel createTitlePanel(String title) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(MAIN_COLOR);
        panel.setPreferredSize(new Dimension(800, 40));
        
        JLabel titleLabel = new JLabel(title, JLabel.CENTER);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        
        panel.add(titleLabel, BorderLayout.CENTER);
        
        return panel;
    }
    
    private JPanel createPasswordPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createTitledBorder("密码修改"));
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 200));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
        formPanel.setBackground(Color.WHITE);
        
        formPanel.add(new JLabel("原密码："));
        oldPasswordField = new JPasswordField();
        formPanel.add(oldPasswordField);
        
        formPanel.add(new JLabel("新密码："));
        newPasswordField = new JPasswordField();
        formPanel.add(newPasswordField);
        
        formPanel.add(new JLabel("确认密码："));
        confirmPasswordField = new JPasswordField();
        formPanel.add(confirmPasswordField);
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(Color.WHITE);
        
        JButton applyButton = new JButton("应用");
        JButton resetButton = new JButton("重置为默认密码");
        
        applyButton.addActionListener(e -> changePassword());
        resetButton.addActionListener(e -> resetPassword());
        
        buttonPanel.add(resetButton);
        buttonPanel.add(applyButton);
        
        JTextArea noteArea = new JTextArea("注意：初始密码为112233，请及时修改密码以保护系统安全。");
        noteArea.setEditable(false);
        noteArea.setLineWrap(true);
        noteArea.setWrapStyleWord(true);
        noteArea.setBackground(panel.getBackground());
        noteArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        panel.add(noteArea, BorderLayout.NORTH);
        panel.add(formPanel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        
        return panel;
    }
    
    private JPanel createVersionPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createTitledBorder("系统版本"));
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 150));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        JPanel infoPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        infoPanel.setBackground(Color.WHITE);
        
        infoPanel.add(new JLabel("主程序版本号："));
        infoPanel.add(new JLabel("V5.0.0.1"));
        
        infoPanel.add(new JLabel("网址库版本号："));
        infoPanel.add(new JLabel("V5.0.0.1"));
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(Color.WHITE);
        
        JButton checkButton = new JButton("检测");
        JButton updateButton = new JButton("更新");
        
        checkButton.addActionListener(e -> checkUpdate());
        updateButton.addActionListener(e -> updateSystem());
        
        buttonPanel.add(checkButton);
        buttonPanel.add(updateButton);
        
        panel.add(infoPanel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        
        return panel;
    }
    
    private void changePassword() {
        String oldPassword = new String(oldPasswordField.getPassword());
        String newPassword = new String(newPasswordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        
        // 验证输入
        if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有密码字段", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (!newPassword.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "新密码与确认密码不匹配", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // 尝试修改密码
        boolean success = passwordManager.changePassword(oldPassword, newPassword);
        if (success) {
            JOptionPane.showMessageDialog(this, "密码修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            oldPasswordField.setText("");
            newPasswordField.setText("");
            confirmPasswordField.setText("");
        } else {
            JOptionPane.showMessageDialog(this, "原密码不正确", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void resetPassword() {
        int result = JOptionPane.showConfirmDialog(this, 
                "确定要将密码重置为默认密码（112233）吗？", 
                "确认重置", 
                JOptionPane.YES_NO_OPTION);
        
        if (result == JOptionPane.YES_OPTION) {
            passwordManager.resetPassword();
            JOptionPane.showMessageDialog(this, 
                    "密码已重置为默认密码：" + PasswordManager.getDefaultPassword(), 
                    "重置成功", 
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private void checkUpdate() {
        // 模拟检查更新
        JOptionPane.showMessageDialog(this, 
                "当前已是最新版本", 
                "检测结果", 
                JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void updateSystem() {
        // 模拟系统更新
        JOptionPane.showMessageDialog(this, 
                "系统更新功能将在后续版本中提供", 
                "提示", 
                JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
     * 获取密码管理器实例
     */
    public PasswordManager getPasswordManager() {
        return passwordManager;
    }
}